package com.bwie.controller;

import cn.hutool.core.bean.BeanUtil;
import com.bwie.es.EsProd;
import com.bwie.mapper.TbProdMapper;
import com.bwie.pojo.TbProd;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.web.bind.annotation.*;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2025/3/28 14:15
 */

@RestController
@RequestMapping("/prod/es")
public class EsIndexController {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    TbProdMapper tbProdMapper;


    @PostMapping("/createIndex")
    public R createIndex() {
        //-- 检查索引是否存在，如果存在，删除索引
        IndexOperations indexOps = elasticsearchRestTemplate.indexOps(EsProd.class);
        boolean exists = indexOps.exists();
        if(exists) {
            // 索引存在，删除索引
            indexOps.delete();
        }

        //-- 创建索引
        indexOps.create();

        //-- 手动创建映射
        Document mapping = indexOps.createMapping();
        indexOps.putMapping(mapping);

        return R.OK();
    }

    /**
     * 把商品信息存储到ES中
     * @param prodId
     * @return
     */
    @PostMapping("/saveProdToEs/{prodId}")
    public R saveProdToEs(@PathVariable("prodId") Integer prodId) {
        TbProd tbProd = tbProdMapper.selectById(prodId);
        if(tbProd == null) {
            return R.ERROR(404, "商品不存在");
        }

        //
        EsProd esProd = BeanUtil.toBean(tbProd, EsProd.class);
        elasticsearchRestTemplate.save(esProd);

        return R.OK();
    }

    @PostMapping("/getProdFromEs/{prodId}")
    public R getProdFromEs(@PathVariable("prodId") Integer prodId) {
        EsProd esProd = elasticsearchRestTemplate.get("" + prodId, EsProd.class);
        if(esProd == null) {
            return R.ERROR(404, "商品不存在");
        }

        return R.OK(esProd);
    }

    @PostMapping("/deleteProdFromEs/{prodId}")
    public R deleteProdFromEs(@PathVariable("prodId") Integer prodId) {
        elasticsearchRestTemplate.delete("" + prodId, EsProd.class);
        return R.OK();
    }

    /**
     * @description 添加商品
     * @params [tbProd]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/3/29 9:05
     */
    @PostMapping("/addProdToEs")
    public R addProdToEs(@RequestBody TbProd tbProd) {

        //-- 添加商品到数据库中
        tbProdMapper.insert(tbProd);

        //-- 把商品信息存入ES中
        EsProd esProd = BeanUtil.toBean(tbProd, EsProd.class);
        elasticsearchRestTemplate.save(esProd);

        return R.OK();
    }

    /**
     * @description 修改商品信息
     * @params [tbProd]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/3/29 9:07
     */
    public R updateProdToEs(@RequestBody TbProd tbProd) {

        //-- 修改商品信息
        tbProdMapper.updateById(tbProd);

        //-- 把商品信息存入ES中
        EsProd esProd = BeanUtil.toBean(tbProd, EsProd.class);
        elasticsearchRestTemplate.save(esProd);
        return R.OK();
    }

}
